package com.bindapp.activity;

import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.bindapp.R;
import com.bindapp.bean.ChangePassword;
import com.bindapp.databinding.ActivitySetnewpassBinding;
import com.bindapp.http.MNetHttp;
import com.bindapp.listener.HttpListener;
import com.bindapp.util.PreferencesUtil;
import com.bindapp.util.Static;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by tom055 on 2018/7/22.
 */

public class SetPassActivity extends BaseActivity {
    private ActivitySetnewpassBinding binding;

    @Override
    protected void after() {
        super.after();
        handerBackToolar("修改密码");
        binding= DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_setnewpass,null,false);
        binding.setClick(new ViewClick());
        AddContentView(binding.getRoot());
    }

    public class ViewClick{
        public void onClick(View view){
            switch(view.getId()){
                case R.id.submit:
                    if(TextUtils.isEmpty(binding.oldview.getText())){
                        Static.showToast(SetPassActivity.this,"旧密码不能为空");
                    }else if(TextUtils.isEmpty(binding.newpass.getText())){
                        Static.showToast(SetPassActivity.this,"新密码不能为空");
                    }else if(TextUtils.isEmpty(binding.newpass2.getText())){
                        Static.showToast(SetPassActivity.this,"新密码不能为空");
                    }else if(binding.newpass.getText().toString().length()<6||binding.newpass.getText().toString().length()>16){
                        Static.showToast(SetPassActivity.this,"密码长度必须在6到16位之间");
                    }else if(!binding.newpass.getText().toString().equals(binding.newpass2.getText().toString())){
                        Static.showToast(SetPassActivity.this,"2次新密码输入不一致");
                    }else{
                        showProgress("正在提交中");
                        Map<String,String> params=new HashMap<String,String>();
                        params.put("PhoneNumber", PreferencesUtil.getInstance(SetPassActivity.this).getPhonenum());
                        params.put("Password",binding.oldview.getText().toString());
                        params.put("NewPassword",binding.newpass2.getText().toString());
                        MNetHttp.getInstance(SetPassActivity.this).rxPostRequest(Static.ChangePassword, params, ChangePassword.class, new HttpListener() {
                            @Override
                            public void success(Object obj) {
                                dissProgress();
                                if(((ChangePassword)obj).isSuccess()&&((ChangePassword)obj).isBl()){
                                    PreferencesUtil.getInstance(SetPassActivity.this).setPassword(binding.newpass.getText().toString());
                                    Static.showToast(SetPassActivity.this,"修改密码成功");
                                    finish();
                                }else{
                                    Static.showToast(SetPassActivity.this,((ChangePassword)obj).getMsg());
                                }
                            }

                            @Override
                            public void fail(String msg) {
                                dissProgress();
                            }
                        });
                    }
                    break;
            }
        }
    }
}
